package msu.ml.gis;

import msu.ml.data.*;
import java.util.*;
import java.io.*;
import java.awt.geom.Rectangle2D;

/**
 * The polygon class represents a polygon
 * in the GIS system.
 *
 * @author Reginald M Mead
 * @version 1.0
 */
public class Polygon extends Shape {

	protected Rectangle2D.Double boundingBox;
	protected ArrayList<Point> m_Point;

	/**
	 * Create a new Polygon
	 */ 
	public Polygon()
	{
		m_Point = new ArrayList<Point>();
	}

	/**
	 * Get the Bounding Box for this bounding box
	 *
	 the reader* @return the bounding box for this bounding box
	 */
	public Rectangle2D getBoundingBox()
	{
		return boundingBox;
	}

	/**
	 * Get the length of this shape record
	 * in 16bit words
	 *
	 * @return length in 16bit words
	 */
	public int getContentLength()
	{
		return 24 + 8 * m_Point.size();
	}

	/**
	 * Get the points (vertices) for 
	 * this polygon.
	 *
	 * @return the points in this polygon
	 */
	public ArrayList<Point> getPoints()
	{
		return m_Point;
	}

	/**
	 * Get the shape type for a polygon
	 *
	 * @return the shape type for a polygon
	 */
	public int getShapeType()
	{
		return 5;
	}

	/**
	 * Set the bounding box for this polygon
	 * 
	 * @param newVal the bounding box for this polygon
	 */
	public void setBoundingBox(Rectangle2D.Double newVal)
	{
		boundingBox = newVal;
	}

	/**
	 * Write this polygon to the specified output stream
	 *
	 * @param out output stream to write polygon to
	 */
	public void serialize(LittleEndianDataOutputStream out) throws IOException
	{
		out.writeDoubleLE(boundingBox.getMinX());
		out.writeDoubleLE(boundingBox.getMinY());
		out.writeDoubleLE(boundingBox.getMaxX());
		out.writeDoubleLE(boundingBox.getMaxY());
		out.writeIntLE(1);
		out.writeIntLE(m_Point.size());
		out.writeIntLE(0);
		for(int i = 0; i < m_Point.size(); i++)
			m_Point.get(i).serialize(out);
	}

}
